import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import SelfServiceNotice from '../views/selfservicenotice.vue'
import HospitalSelection from '../views/HospitalSelection.vue'
import OrderApplication from '@/views/OrderApplication.vue'
import OrderHistory from '@/views/OrderHistory.vue'
import OrderDetail from '@/views/OrderDetail.vue'
import Profile from '@/views/Profile.vue'

/**
 * 路由配置
 * 智慧医疗平台移动端路由
 */
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/home',
      name: 'home',
      component: Home,
      meta: { 
        title: '智慧医疗平台',
        requiresAuth: true 
      }
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: {
        title: '用户登录',
        requiresAuth: false
      }
    },
    {
      path: '/register',
      name: 'register',
      component: Register,
      meta: {
        title: '用户注册',
        requiresAuth: false
      }
    },
    {
      path: '/self-service-notice',
      name: 'selfServiceNotice',
      component: SelfServiceNotice,
      meta: {
        title: '自助开单须知',
        requiresAuth: true
      }
    },
    {
      path: '/hospital-selection',
      name: 'hospitalSelection',
      component: HospitalSelection,
      meta: {
        title: '院区选择',
        requiresAuth: true
      }
    },
    {
      path: '/order-application',
      name: 'orderApplication',
      component: OrderApplication,
      meta: { 
        title: '开单申请',
        requiresAuth: true 
      }
    },
    {
      path: '/order-history',
      name: 'orderHistory',
      component: OrderHistory,
      meta: { 
        title: '开单记录',
        requiresAuth: true 
      }
    },
    {
      path: '/order-detail/:id',
      name: 'orderDetail',
      component: OrderDetail,
      meta: { 
        title: '开单详情',
        requiresAuth: true 
      }
    },
    {
      path: '/order-confirmation',
      name: 'OrderConfirmation',
      component: () => import('../views/OrderConfirmation.vue'),
      meta: { 
        title: '开单确认',
        requiresAuth: true 
      }
    },
    {
      path: '/order-payment',
      name: 'OrderPayment',
      component: () => import('../views/OrderPayment.vue'),
      meta: { 
        title: '订单支付',
        requiresAuth: true 
      }
    },
    {
      path: '/profile',
      name: 'profile',
      component: Profile,
      meta: { 
        title: '我的',
        requiresAuth: true 
      }
    }
  ],
})

/**
 * 全局路由守卫
 * 检查用户登录状态，保护需要授权的页面
 */
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title as string
  }

  // 检查是否需要登录权限
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
  
  // 检查用户是否已登录
  const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'
  
  if (requiresAuth && !isLoggedIn) {
    // 需要登录但未登录，重定向到登录页
    next({
      path: '/login',
      query: { redirect: to.fullPath } // 记录原始路径，登录后跳转回去
    })
  } else if (to.path === '/login' && isLoggedIn) {
    // 已登录用户访问登录页，重定向到首页
    next('/home')
  } else {
    // 正常访问
    next()
  }
})

export default router
